package com.alibaba.citrus.cr.platform.enhance.repository.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import com.alibaba.citrus.cr.common.annotation.RepositoryInvoker;
import com.alibaba.citrus.cr.platform.enhance.repository.SalesOrderQueryServiceRepository;
import com.alibaba.citrus.cr.platform.enhance.repository.model.diff.activity.QuerySalesOrderParam;
import com.alibaba.citrus.cr.platform.enhance.repository.model.diff.common.SalesOrderDTO;
import com.alibaba.lattice2.epoch.sdk.model.Result;

import com.epoch.app.crb2btradecenter.domain.order.dto.QueryByGmtModifyRequest;
import com.epoch.app.crb2btradecenter.domain.order.dto.QueryByIdRequest;
import com.epoch.app.crb2btradecenter.domain.order.model.Order;
import com.epoch.app.crb2btradecenter.domain.order.service.OrderService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Repository;

/**
 * @author hanguo
 * @date 2022/1/24
 * @Description 销售主单查询服务资源层
 */
@Repository
public class SalesOrderQueryServiceRepositoryImpl implements SalesOrderQueryServiceRepository {

    @Resource
    private OrderService orderService;

//    @Override
//    @RepositoryInvoker
//    public SalesOrderDTO queryBySap(MnSapIf017SDO sapIf017SDO) {
//        return null;
//    }

    @Override
    @RepositoryInvoker
    public SalesOrderDTO queryById(String salesOrderId) {
        QueryByIdRequest queryByIdRequest = new QueryByIdRequest();
        queryByIdRequest.setId(salesOrderId);
        Result<Order> orderResult = orderService.queryById(queryByIdRequest);
        if (Objects.isNull(orderResult) || Objects.isNull(orderResult.getResult())
                || Objects.isNull(orderResult.getResult().getId())) {
            return null;
        }
        Order order = orderResult.getResult();
        SalesOrderDTO salesOrderDTO = new SalesOrderDTO();
        BeanUtils.copyProperties(order, salesOrderDTO);

        return salesOrderDTO;
    }

    /**
     *  分页查询销售主单数据
     * @param salesOrderParam
     * @return
     */
    @Override
    @RepositoryInvoker
    public Result<List<SalesOrderDTO>> queryByGmtModify(QuerySalesOrderParam salesOrderParam) {
        QueryByGmtModifyRequest request = new QueryByGmtModifyRequest();
        request.setStart(salesOrderParam.getStart());
        request.setLimit(salesOrderParam.getLimit());
        request.setGmtModifyStartDate(salesOrderParam.getStartModifyTime());
        request.setGmtModiftEndDate(salesOrderParam.getEndModifyTime());

        Result<List<Order>> listResult = orderService.queryByGmtModify(request);

        if(CollectionUtils.isEmpty(listResult.getResult())){
            return Result.success(new ArrayList<>());
        }

        List<SalesOrderDTO> salesOrderDTOS = listResult.getResult().stream().map(e -> {
            SalesOrderDTO salesOrderDTO = new SalesOrderDTO();
            BeanUtils.copyProperties(e, salesOrderDTO);
            return salesOrderDTO;
        }).collect(Collectors.toList());

        Result<List<SalesOrderDTO>> result = Result.success(salesOrderDTOS);
        result.setTotal(listResult.getTotal());
        return result;
    }
}
